package com.xiaoyi.service.impl;

import com.google.gson.Gson;
import com.xiaoyi.dao.SysRegionDao;
import com.xiaoyi.dao.impl.SysRegionDaoImpl;
import com.xiaoyi.entity.RespBean;
import com.xiaoyi.entity.SysRegion;
import com.xiaoyi.service.SysRegionService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class SysRegionServiceImpl implements SysRegionService {
    //在service层引入数据访问层(Dao)
    private SysRegionDao sysRegionDao=new SysRegionDaoImpl();

    /**
     * 业务层 查询地址
     * @param req
     * @return
     */
    @Override
    public String queryAddress(HttpServletRequest req) {
        RespBean resp;//数据组装成respBean
        //获取请求参数
        String parentId = req.getParameter("parentId");
        //根据parentId查询对应的地理位置集合
        List<SysRegion> addressList=sysRegionDao.getAddressByParentId(parentId);
        if(addressList!=null&&addressList.size()>0){
            //查到数据量，设置响应码为200，message为success data为addressList
            resp=RespBean.success(addressList);
        }else {
            //查不到数据 设置响应码为10086 message为查询失败
            resp=RespBean.error(10086,"查询失败");
        }
        //将组装好的respBean对象转成JSON字符串
        return new Gson().toJson(resp);
    }
}
